//------------------------------------------------------------------------------------
#include <QtDebug>
#include "CSnake.h"
#include "common.h"
//------------------------------------------------------------------------------------
CSnake::CSnake(int len) {
    head = new CSnakeHead();
    body = new QList<CSnakeBody *>();

    setIncY(-1);

    for(int i=0;i<len;i++) {
        incLen();
    }
}
//------------------------------------------------------------------------------------
CSnake::~CSnake(void) {
    delete head;

    for(int i=0;i<body->size();i++) {
        delete body->at(i);
    }

    body->clear();
    delete body;
}
//------------------------------------------------------------------------------------
void CSnake::draw(QPainter *painter, int idxImage) {
    head->draw(painter, idxImage % head->getNbImage());

    for(int i=0;i<body->size();i++) {
        body->at(i)->draw(painter, idxImage % body->at(i)->getNbImage());
    }
}
//------------------------------------------------------------------------------------
void CSnake::incLen(void) {
    CSnakeBody *snakeBody=new CSnakeBody();
    QPoint pos;
	int incX=0;
	int incY=1;
    
    if(body->size() >= 2) {
		QPoint posBefore(body->at(body->size()-2)->getPosition());
		pos = body->last()->getPosition();
		incX = pos.x()-posBefore.x();
		incY = pos.y()-posBefore.y();
    }else {
		pos = head->getPosition();
    }

    pos.rx()+=incX;
	pos.ry()+=incY;

    snakeBody->setPosition(pos);

    body->append(snakeBody);
}
//------------------------------------------------------------------------------------
void CSnake::setPosition(QPoint position) {
    head->setPosition(position);
    QPoint bPos(position);

    for(int i=0;i<body->size();i++) {
        bPos.ry()+=1;
        body->at(i)->setPosition(bPos);
    }
}
//------------------------------------------------------------------------------------
void CSnake::next(void) {
	if(body->size() > 0) {
		CSnakeBody *last=body->takeLast();
		last->setPosition(head->getPosition());
		body->insert(0, last);
	}

    head->next();
}
//------------------------------------------------------------------------------------
void CSnake::setIncX(int incX) {
    if(head->getIncX() != incX) {
         head->setIncX(incX);
    }
}
//------------------------------------------------------------------------------------
void CSnake::setIncY(int incY) {
    if(head->getIncY() != incY) {
        head->setIncY(incY);
    }
}
//------------------------------------------------------------------------------------
bool CSnake::onCell(int index) {
    if(head->getIndex() == index) {
        return true;
    }

    for(int i=0;i<body->size();i++) {
        if(body->at(i)->getIndex() == index) {
            return true;
        }
    }

    return false;
}
//------------------------------------------------------------------------------------
int CSnake::getIndex(void) {
    return head->getIndex();
}
//------------------------------------------------------------------------------------
bool CSnake::onBody(void) {
    int headIndex = head->getIndex();

    for(int i=0;i<body->size();i++) {
		if(body->at(i)->getIndex() == headIndex) {
            return true;
        }
    }

    return false;
}
//------------------------------------------------------------------------------------
